// pages/pallet_details/pallet_details.js
var app = getApp();
var util = require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderinfo: null,
    time: '00:00',
    date:'2018-01-01'
  },

  call: function (res) {
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.num //仅为示例，并非真实的电话号码
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  // 获取订单详情
  getOrderInfo: function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/order/' + that.data.orderId,
        data:{
          token: session,
        },
        success: function (res) {
          if(res.statusCode == 200){
            if (res.data.msg == 'OK'){
              console.log(res.data.data);
              that.setData({
                data: that.changestatus(res.data.data)
              })
              that.data.data.order.sendstop = util.formatDate(new Date((parseInt(that.data.data.cargo.sendDate2) / 1000 + 691200) * 1000), 'YYYY-MM-dd hh:mm:ss');
              that.data.data.order.deliverysend = util.formatDate(new Date((parseInt(that.data.data.order.loadOn) / 1000 + 691200) * 1000), 'YYYY-MM-dd hh:mm:ss');
              that.data.data.cargo.sendDate1 = util.formatDate(new Date(that.data.data.cargo.sendDate1), 'YYYY-MM-dd hh:mm:ss');
              that.data.data.cargo.sendDate2 = util.formatDate(new Date(that.data.data.cargo.sendDate2), 'YYYY-MM-dd hh:mm:ss');
              that.data.data.order.deliveryOn = util.formatDate(new Date(that.data.data.order.deliveryOn), 'YYYY-MM-dd hh:mm:ss');
              that.data.data.order.unloadOn = util.formatDate(new Date(that.data.data.order.unloadOn), 'YYYY-MM-dd hh:mm:ss');
              that.setData({
                data: that.data.data
              })
            }else{
              wx.showModal({
                title: '提示',
                content: res.data.msg,
              })
            }
          }else{
            wx.showModal({
              title: '提示',
              content: '接口请求错误：错误码：' + res.statusCode
            })
          }
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options:options
    })
    if (options.id){
      this.setData({
        orderId: options.id
      })
      this.getOrderInfo();
    }else{
      wx.showModal({
        title: '提示',
        content: '订单号不能为空',
      })
    }
  },
  //更改状态
  changestatus(list) {
    if (list.status == 7 && list.order.loadWeight != 0 && list.order.unloadWeight == 0) {
      list.status = 5;//运输中-->已送达
    } else if (list.status == 7 && list.order.loadWeight == 0) {
      list.status = 3;//待提货-->提货完成
    } else if (list.status == 7 && list.order.unloadWeight != 0 && list.order.loadWeight != 0 && list.order.position == null) {
      list.status = 6;//等待入库-->入库卸货
    } else if (list.status == 1) {
      list.status = 1;//待接单-->取消派单
    } else if (list.status == 3 && list.order.loadWeight != 0 && list.order.unloadWeight == 0) {
      list.status = 5;//已超时,运输中
    } else if (list.status == 3 && list.order.loadWeigh == 0) {
      list.status = 3;//已超时，待提货-->提货完成
    } else if (list.status == 3 && list.order.unloadWeight != 0 && list.order.loadWeight != 0 && list.order.position == null) {
      list.status = 6;//已超时，等待入库-->入库卸货
    } else if (list.status == 8) {
      list.status = 4;//已完成-->取消派单
    }
    return list
  },
  //输入接单重量
  input:function(res){
    var that = this;
    that.data.data.order.goodWeight=res.detail.value;
    that.data.data.cargo.remain = that.data.data.cargo.remain == null ? that.data.data.cargo.gross : that.data.data.cargo.remain
    that.setData({
      data:that.data.data,
    })
  },
  //确认接单
  suerorder:function(res){
    var that = this;
    var time = that.data.date + ' ' + that.data.time;
    console.log(time);
    time = Date.parse(new Date(time));
    var startTime = Date.parse(new Date(that.data.data.cargo.sendDate1));
    var endTime = Date.parse(new Date(that.data.data.cargo.sendDate2));
    if (time < startTime || time > endTime) {
      wx.showModal({
        title: '提示',
        content: '您选择时间不在范围内',
      })
    } else if (!/^\d+(\.\d+)?$/.test(that.data.data.order.goodWeight) || that.data.data.order.goodWeight>that.data.data.cargo.remain){
      wx.showModal({
        title: '提示',
        content: '请输入正确的接单重量！',
      })
    }else{
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api + '/wx_log_test/order/update?token=' + session,
          data: {
            loadOn:null,
            loadPicUrl:null,
            loadWeight:0,
            orderId: that.data.options.id,
            position:null,
            prePosition:null,
            suspensionInfo: "string",
            unLoadPicUrl:null,
            unloadOn:null,
            unloadWeight:0,
            deliveryOn:time,
            goodWeight: that.data.data.order.goodWeight,
          },
          method: 'POST',
          success: function (res) {
            var res = res.data;
            if (res.msg == "OK") {
              wx.showToast({
                title: '接单成功！！！',
                icon: 'success',
                success: function (res) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showToast({
                title: '请重试！',
                icon: 'loading',
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '请重试！',
              icon: 'loading'
            })
          }
        })
      })
    }
  },
  //取消接单
  closeorder:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api+'/wx_log_test/order/unappoint?token='+session+'&&orderid='+that.data.data.order.id,
        method:'POST',
        success:function(res){
          var res = res.data;
          console.log(res);
          if(res.data==true){
            wx.showModal({
              title: '温馨提示',
              content: '取消接单成功！',
              showCancel:false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta:1
                  })
                }
              }
            })
          }else{
            wx.showToast({
              title: '失败，请重试！',
              icon:'loading'
            })
          }
        },fail:function(res){
          wx.showToast({
            title: '失败，请重试！',
            icon: 'loading'
          })
        }
      })
    })
  }
})